package httpclient.signature.impl;

import httpclient.signature.IUrlSignature;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * Created by yangqy on 2017/2/15.
 */
public class DefaultUrlSignatureImpl implements IUrlSignature {

    private String key;

    public DefaultUrlSignatureImpl(String key) {
        this.key = key;
    }


    public Map<String, String> urlSignatureArgs() {
        String timestamp = String.valueOf(new Date().getTime());
        String[] nonce = UUID.randomUUID().toString().split("-");
        String tmpNonce = nonce[nonce.length - 1];

        List<String> tmpList = new ArrayList();
        tmpList.add(key);
        tmpList.add(timestamp);
        tmpList.add(tmpNonce);
        Collections.sort(tmpList);

        String tmpStr = StringUtils.join(tmpList.toArray());
        String signature = DigestUtils.sha1Hex(tmpStr);
        Map map = new HashMap();
        map.put("_timestamp", timestamp);
        map.put("_nonce", tmpNonce);
        map.put("_signature", signature);
        return map;
    }
}